package com.darren.euc.action;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.darren.euc.entity.Member;
import com.darren.euc.entity.PrintRecord;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.service.MemberService;
import com.darren.euc.service.PrintRecordService;
import com.darren.euc.util.StringUtil;

import Com.FirstSolver.Security.Base64;


@Controller
@RequestMapping
public class CommonAction {

	private static final Logger log = Logger.getLogger(CommonAction.class);
	
    @Autowired
    private MemberService memberService;
    
    @Autowired
    private PrintRecordService printRecordService;
	
	@ResponseBody
    @RequestMapping(value = "/externalLogin",method = RequestMethod.POST)
	public String externalLogin(@RequestParam(name = "loginAccount", required = false) String loginAccount,
			@RequestParam(name = "loginPassword", required = false) String loginPassword,
			@RequestParam(name = "loginDomain", required = false) String loginDomain,
			@RequestParam(name = "loginCard", required = false) String loginCard,
			@RequestParam(name = "serialNumber", required = false) String serialNumber,
			HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		String result = "";
		log.info(String.format("loginAccount:%s \t loginPassword:%s \t loginDomain:%s \t loginCard:%s \t serialNumber:%s \t", loginAccount,loginPassword,loginDomain,loginCard,serialNumber));
		Member member = null;
		if (StringUtil.isEmptyOrWhitespace(loginAccount) && StringUtil.isEmptyOrWhitespace(loginPassword)
				&& StringUtil.isEmptyOrWhitespace(loginCard)) {
			result = "FAIL:ERR-00001";
		}else if(!StringUtil.isEmptyOrWhitespace(loginAccount) && !StringUtil.isEmptyOrWhitespace(loginPassword)){
			try{
				member = memberService.login(loginAccount, loginPassword);
			}catch(Exception ex){
				result = "FAIL:ERR-00001";
			}
		}else{
			member = memberService.findByIcCard(loginCard.trim());
			if(member == null){
				result = "FAIL:ERR-00001";
			}
		}
		if("".equals(StringUtil.convertNullToEmptyString(result))){
//			result = String.format("OK:<xml><user_name>%s</user_name><card_id>%s</card_id><group_name></group_name><email_address>%s</email_address><feature_list><allow_print>Y</allow_print><allow_copy>Y</allow_copy><allow_color>Y</allow_color><allow_scan>Y</allow_scan><allow_fax>Y</allow_fax></feature_list><copy_mono_limit>-1</copy_mono_limit><copy_color_limit>-1</copy_color_limit></xml>",
//					StringUtil.convertNullToEmptyString(member.getMobile()).trim(),
//					StringUtil.convertNullToEmptyString(member.getIcCard()).trim(),
//					StringUtil.convertNullToEmptyString(member.getEmail()).trim());
			result = String.format("OK:<xml><user_name>%s</user_name><card_id>%s</card_id><group_name></group_name><email_address>%s</email_address><feature_list><allow_print>Y</allow_print><allow_copy>Y</allow_copy><allow_color>Y</allow_color><allow_scan>Y</allow_scan><allow_fax>Y</allow_fax></feature_list></xml>",
					StringUtil.convertNullToEmptyString(member.getMobile()).trim(),
					StringUtil.convertNullToEmptyString(member.getIcCard()).trim(),
					StringUtil.convertNullToEmptyString(member.getEmail()).trim());
		}
		log.info(result);
		result = Base64.Encode(result.getBytes("UTF-8"));
		log.info(result);
        return result;
    }

	@ResponseBody
    @RequestMapping(value = "/mfpLogNotification",method = RequestMethod.POST)
    public String mfpLogNotification(@RequestParam(name = "jobData", required = false) String jobData,
    		HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		String result = "FAIL";
		log.info(jobData);
		try{
			PrintRecord printRecord = JSONObject.parseObject(jobData, PrintRecord.class);
			printRecordService.save(printRecord);
			result = "OK";
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
		}
		result = Base64.Encode(result.getBytes("UTF-8"));
		return result;
    }
	
	
}
